package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    /**
     * 根据id删除地址信息
     * @param id
     */
    public void deleteAddress(Integer id) {
        addressDao.deleteAddress(id);
    }

    /**
     * 查询所有地址名称
     * @return
     */
    public List<String> find() {
        return addressDao.find();
    }

    /**
     * 分页查询所有地址
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //使用分页插件
        PageHelper.startPage(currentPage, pageSize);
        Page<Address> addresses = addressDao.selectCount(queryString);

        return new PageResult(addresses.getTotal(), addresses.getResult());
    }

    /**
     * 根据id查询查询地址id
     * @param address
     * @return
     */
    public Integer findId(String address) {
        return addressDao.findId(address);
    }


    /**
     * 添加地址
     * @param address
     */
    public void addAddress(Address address) {
        addressDao.addAddress(address);
    }


}
